package com.twu.biblioteca.ui;

import com.twu.biblioteca.data.*;

import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Collection;

/**
 * Created by florian on 26/12/13.
 */
public class BibliotecaUI {
    private final PrintStream printer;
    private static final String BOOK_TABLE_FORMAT = "%-5s | %-16s | %-15s\n";
    private static final String MOVIE_TABLE_FORMAT = "%-5s | %-20s | %-4s | %-20s | %-6s\n";
    private static final String USER_INFO_FORMAT = "%-18s %s\n";
    public BibliotecaUI(PrintStream printer) {
        this.printer = printer;
    }

    public void showWelcomeMessage() {
        printer.println("Welcome to Biblioteca");
    }

    public void listBooks(Collection<BibliotecaItem> items) {
        printer.format(BOOK_TABLE_FORMAT,"ID","Author","Title");
        printer.println("---------------------------------------------------------");
        for (BibliotecaItem item: items) {
            Book book = (Book) item;
            printer.format(BOOK_TABLE_FORMAT, book.getId(), book.getAuthor(), book.getTitle());
        }
    }

    public void listMovies(Collection<BibliotecaItem> items) {
        printer.format(MOVIE_TABLE_FORMAT,"ID","Name", "Year", "Director", "Rating");
        printer.println("-------------------------------------------------------------------");
        for (BibliotecaItem item : items) {
            Movie movie = (Movie) item;
            printer.format(MOVIE_TABLE_FORMAT, movie.getId(), movie.getName(), movie.getYear(), movie.getDirector(), movie.getRating());
        }
    }

    public void showLoggedInMainMenu() {
        printer.println("Main Menu (logged in)\n" +
                "---------------------------------------------------------\n" +
                "1\tList available books\t\n" +
                "2\tCheckout book\n" +
                "3\tReturn book\n" +
                "4\tList available movies\n"+
                "5\tCheckout movie\n"+
                "6\tShow user info\n"+
                "q\tQuit");
        printer.print("Please select an option: ");
    }

    public void showMainMenu() {
        printer.println("Main Menu\n" +
                "---------------------------------------------------------\n" +
                "1\tList available books\t\n" +
                "4\tList available movies\n"+
                "10\tLog in\n"+
                "q\tQuit");
        printer.print("Please select an option: ");
    }

    public void showUserInfo(User user) {
        printer.format(USER_INFO_FORMAT,"Library Number:",user.getLibraryNumber());
        printer.format(USER_INFO_FORMAT,"Name:",user.getName());
        printer.format(USER_INFO_FORMAT,"E-Mail:",user.getEmail());
        printer.format(USER_INFO_FORMAT,"Phone:",user.getPhoneNumber());
    }

    public void showInvalidMenuOptionMessage() {
        printer.print("ERROR: You selected an invalid option! Please select another option: ");
    }


    public void showBookCheckoutMessage() {
        printer.print("Please enter a book ID you want to check out: ");
    }


    public void showMovieCheckoutMessage() {
        printer.print("Please enter a movie ID you want to check out: ");
    }

    public void showUnsuccessfulCheckoutBookMessage() {
        printer.println("That book is not available.");
    }

    public void showSuccessfulCheckoutBookMessage() {
        printer.println("Thank you! Enjoy the book!");
    }

    public void showUnsuccessfulCheckoutMovieMessage() {
        printer.println("That movie is not available.");
    }

    public void showSuccessfulCheckoutMovieMessage() {
        printer.println("Thank you! Enjoy the movie!");
    }

    public void showReturnMessage() {
        printer.print("Please enter a book ID you want to return: ");
    }

    public void showUnsuccessfulReturnMessage() {
        printer.println("That is not a valid book to return.");
    }

    public void showSuccessfulReturnMessage() {
        printer.println("Thank you for returning the book!");
    }

    public void showLoginMessage() {
        printer.println("Please enter your Library Number (xxx-xxxx) and your Password");
        printer.print("Library Number: ");
    }

    public void showPasswordMessage() {
        printer.print("Password: ");
    }

    public void showLoginSuccessful() {
        printer.println("Login Successful!");
    }

    public void showLoginUnsuccessful() {
        printer.println("Login Unsuccessful, please try again!");
    }


}
